package com.york.oss.config;

import com.york.oss.adapter.StorageAdapter;
import com.york.oss.adapter.AliStorageAdapter;
import com.york.oss.adapter.MinioStorageAdapter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 文件存储config
 */
@Configuration
// 自动刷新 @RefreshScope
@RefreshScope
public class StorageConfig {

    @Value("minio")
    private String storageType;

    /**
     * 方法名是注入容器中的对象名，因此该方法名在FileService中就配对了
     */
    @Bean
    @RefreshScope
    public StorageAdapter storageService() {
        if ("minio".equals(storageType)) {
            return new MinioStorageAdapter();
        } else if ("aliyun".equals(storageType)) {
            return new AliStorageAdapter();
        } else {
            throw new IllegalArgumentException("未找到对应的文件存储处理器");
        }
    }

}
